﻿@inject INavInvReportClassService NavInvReportClassService


<div align="center">
    <label>Monthly Average Inventory by Year</label>
</div>
<Chart @ref="_barChart" ChartType="ChartType.Bar" OnInitAsync="@OnInit" Height=100% />
@code {
    private Chart _barChart = default!;
    private List<ChartDoubleDataDto> rawMaterialDataDtos = new();
    private List<ChartDoubleDataDto> semiProductsDataDtos = new();
    private List<ChartDoubleDataDto> finishGoodsTradeDataDtos = new();
    
    private async Task<ChartDataSource> OnInit()
    {
        var response = await NavInvReportClassService.GetAllAsync();
        if (response.Status)
        {
            var group = response.Result.GroupBy(x => x.Year);

            rawMaterialDataDtos = group.Select(x=>new ChartDoubleDataDto
            {
                Label = x.Key.ToString(),
                Value = x.Average(x=>x.RawMtlValue)
            }).ToList();
            semiProductsDataDtos = group.Select(x=>new ChartDoubleDataDto
            {
                Label = x.Key.ToString(),
                Value = x.Average(x=>x.SemiProdValue)
            }).ToList();
            finishGoodsTradeDataDtos = group.Select(x=>new ChartDoubleDataDto
            {
                Label = x.Key.ToString(),
                Value = x.Average(x=>x.FgTradeValue)
            }).ToList();
        }
        var ds = new ChartDataSource();
        ds.Options.X.Title = "Year";
        ds.Options.Y.Title = "Monthly Average Inventory";
        ds.Options.X.Stacked = true;
        ds.Options.Y.Stacked = true;
        ds.Labels = rawMaterialDataDtos.Select(x => x.Label);
        ds.Data.Add(new ChartDataset
        {
            Label = "RawMaterial",
            Data = rawMaterialDataDtos.Select(x => x.Value).Cast<object>(),
        });
        ds.Data.Add(new ChartDataset
        {
            Label = "Semi-Products",
            Data = semiProductsDataDtos.Select(x => x.Value).Cast<object>(),
        });
        ds.Data.Add(new ChartDataset
        {
            Label = "FinishedGoods/Trade",
            Data = finishGoodsTradeDataDtos.Select(x => x.Value).Cast<object>(),
        });
        
        return ds;
    }

}
